/**
 * Created by Lenovo on 2018/1/8.
 */
function checkForm() {
    //>>非空校验
    //var res1 = checkNull("user-name", "用户名不能为空!");
    var res2 = checkNull("u-name", "姓名不能为空!");
    var res3 = checkNull("user-password", "密码不能为空!");
    //var res3 = checkNull("phone", "电话不能为空!");
    //var res4 = checkNull("user-cardid", "身份证号不能为空!");
    var res5 = checkNull("userAddr", "地址不能为空!");
    return res2 &&res3 &&  res5;
}
//函数非空验证
function checkNull(name, msg) {
    var value = $("input[id='" + name + "']").val().trim();
    setMsg(name, "");
    if (value == "") {
        setMsg(name, msg);
        return false;
    }
    return true;
}

//设置提示信息
function setMsg(name, msg) {
    var $oInp = $("input[id='" + name + "']");
    //var $oSpan = $oInp.next("span");
    var $oSpan = $oInp.parent().next().children("span");
    $oSpan.html(msg)
    $oSpan.css("color", "red");
}

//通过正则表达式来验证输入的合法性
function checkCardId(name, msg) {
    var cardid = $("input[id='" + name + "']").val().trim();
    var regExp = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/;
    checkNull("user-cardid", "身份证号不能为空!");

    if (cardid != "" && !regExp.test(cardid)) {
        setMsg(name, msg);
        return false;
    }
    return true;
}
//通过正则表达式验证手机号合法性
function checkPhone(name, msg) {
    var phone = $("input[id='" + name + "']").val().trim();
    var regExp = /^1[34578]\d{9}$/;
    checkNull("phone", "手机号不能为空!");

    if (phone != "" && !regExp.test(phone)) {
        setMsg(name, msg);
        return false;
    }
    return true;
}
//校验用户昵称的合法性
function checkUsername(name) {
    var username = $("input[id='" + name + "']").val().trim();
    if (username=="") {
        $("#sn").html("昵称不能为空");
        $("#sn").css("color", "red");
        return;
    } else{
        //通过ajax从后台获取用户昵称检验用户昵称的唯一性
        $.ajax({
            url: "/Test/useradmin/checkuser",
            data: "username=" + username,
            type: "GET",
            dataType: "json",
            success: function (data) {
                if (data.success){
                    $("#sn").html("该用户名可用");
                    $("#sn").css("color", "green");
                } else {
                    $("#sn").html("该用户名已注册");
                    $("#sn").css("color", "red");
                }
            }
        });
    }
}
$(function () {
    //从url地址获取对应的参数值
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

    var userId = getQueryString('userId');
    var pageIndex = getQueryString("pageIndex");
    if(pageIndex == null){
        pageIndex = 1;
    }
    //设置标志位检验是否为编辑操作
    var isEdit = userId ? true:false;
    var registerUserUrl = '/Test/useradmin/registeruser';
    var userInfoUrl = "/Test/useradmin/getuserbyid?userId=" + userId;
    var editUserUrl = '/Test/useradmin/modifyuser';

    if(isEdit){
        $('.text-center').html("修改用户信息")
        getUserInfo(userId);
    }

    //通过数据库查询将用户信息输出到对应位置
    function getUserInfo(userId) {
        $.getJSON(userInfoUrl, function(data) {
            if (data.success) {
                var user = data.user;
                $('#user-name').val(user.userName).attr('disabled', 'disabled');
                $('#u-name').val(user.uName);
                $('#user-password').val(user.userPassword);
                $("input[name=gender][value="+user.userSex+"]").attr("checked",true);
                $('#user-nation').val(user.userNation);
                $("#user-nation option[data-id='" + user.userNation + "']").attr(
                    "selected", "selected");
                $('#phone').val(user.phone);
                $('#user-cardid').val(user.userCardId);
                $('#user-addr').val(user.userAddr);

            }
        })
    }
    //点击事件，当用户点击提交按钮时，触发该事件
    $('#submit').click(function () {
        var user = {};
        user.userName = $('#user-name').val();
        user.uName = $('#u-name').val();
        user.userPassword=$('#user-password').val();
        user.userSex = $('.radio:checked').val();
        user.userNation = $('#user-nation').val();
        user.phone = $('#phone').val();
        user.userCardId = $('#user-cardid').val();
        user.userAddr = $('#user-addr').val();
        var formData = new FormData();
        formData.append('userStr', JSON.stringify(user));

        //判断请求为修改请求还是注册请求
        $.ajax({
            url: (isEdit ? editUserUrl : registerUserUrl),
            type: 'POST',
            data: formData,
            contentType: false,
            processData: false,
            cache: false,
            dataType: "json",
            success: function (data) {
                if (data.success) {
                    window.location.href = "userlist?pageIndex="+pageIndex;
                } else {
                    $.toast('提交失败！' + data.errMsg);
                }
            }

        });

    });

});

